<?php

namespace app\api\controller;


use app\api\logic\UserExpenseApplicationLogic;
use app\api\validate\UserExpenseApplicationValidate;

class UserExpenseApplicationController extends BaseApiController
{
    /**
     * @notes 用户提交报销申请
     * @return \think\response\Json
     */
    public function apply()
    {
        $params = (new UserExpenseApplicationValidate())->post()->goCheck('apply');
        $userId = $this->request->userId;
        $result = UserExpenseApplicationLogic::apply($params, $userId);
        if ($result['success']){
            return $this->success('申请提交成功', $result['data']);
        } else {
            return $this->fail($result['message']);
        }
    }

    /**
     * @note 用户修改报销申请
     * @return \think\response\Json
     */
    public function modify()
    {
        $params = (new UserExpenseApplicationValidate())->post()->goCheck('modify');
        $userId = $this->request->userId;
        $result = UserExpenseApplicationLogic::modify($params, $userId);
        if ($result['success']){
            return $this->success('修改成功', $result['data']);
        } else {
            return $this->fail($result['message']);
        }
    }

    /**
     * @notes 获取申请详情
     * @return \think\response\Json
     */
    public function detail()
    {
        $id = $this->request->get('id/d', 0);
        $result = UserExpenseApplicationLogic::detail($id, $this->userId);
        if ($result['success']) {
            return $this->success('获取成功', $result['data']);
        }
        return $this->fail($result['message']);
    }

    /**
     * @notes 获取申请列表
     */
    public function lists()
    {
        $lists = new \app\api\lists\userreimbursement\UserExpenseApplicationLists();
        $data = $lists->lists();
        return $this->success('', $data);
    }

    /**
     * @notes 获取操作日志
     * @return \think\response\Json
     */
    public function operationLogs()
    {
        $expenseId = $this->request->get('expense_id/d');
        if (empty($expenseId)) {
            return $this->fail('请提供报账申请ID');
        }

        $result = UserExpenseApplicationLogic::getOperationLogs($expenseId, $this->userId);
        if ($result['success']) {
            return $this->success('获取成功', $result['data']);
        }
        return $this->fail($result['message']);
    }

    /**
     * @notes 获取费用类型列表（用于下拉选择）
     * @return \think\response\Json
     */
    public function getExpenseTypes()
    {
        $result = UserExpenseApplicationLogic::getExpenseTypes();
        if ($result['success']) {
            return $this->success('获取成功', $result['data']);
        }
        return $this->fail($result['message']);
    }
}